local kl__jijiao = fk.CreateSkill({
  name = "kl__jijiao",
  tags = { Skill.Compulsory }
})

Fk:loadTranslationTable {
  ["kl__jijiao"] = "击蛟",
  [":kl__jijiao"] = "锁定技，你始终处于【酒】状态，你的【酒】均视为【冰杀】。游戏开始时，你将3张【冰杀】加入牌堆，然后你根据你的手牌颜色数从牌堆随机获得并使用一张对应攻击范围的武器牌。",

  ["$kl__jijiao1"] = "夫英雄者，在志高，而不在年高。",
}

local U = require "packages.utility.utility"

local spec = {
  on_use = function(self, event, target, player, data)
    player.drank = 1
    player.room:broadcastProperty(player, "drank")
  end,
  on_refresh = function(self, event, target, player, data)
    player.drank = 1
    player.room:broadcastProperty(player, "drank")
  end,
}

kl__jijiao:addEffect(fk.GamePrepared, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(kl__jijiao.name)
  end,
  on_use = spec.on_use,
})
kl__jijiao:addEffect(fk.AfterCardUseDeclared, {
  can_refresh = function(self, event, target, player, data)
    return target == player and player:hasSkill(kl__jijiao.name) and player.drank == 0
  end,
  on_refresh = spec.on_refresh,
})
kl__jijiao:addEffect(fk.EventTurnChanging, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(kl__jijiao.name) and player.drank == 0
  end,
  on_refresh = spec.on_refresh,
})

kl__jijiao:addEffect("filter", {
  card_filter = function(self, card, player, isJudgeEvent)
    return player:hasSkill(kl__jijiao.name) and card.trueName == "analeptic" and (table.contains(player.player_cards[Player.Hand], card.id) or isJudgeEvent)
  end,
  view_as = function(self, plain, card)
    local c = Fk:cloneCard("ice__slash", card.suit, card.number)
    c.skillName = kl__jijiao.name
    return c
  end,
})

kl__jijiao:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(kl__jijiao.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = "ice__slash"
    local numbers = {7, 7, 8, 8, 8}
    local cards = {
      {name, Card.Spade, table.remove(numbers, math.random(#numbers))},
      {name, Card.Spade, table.remove(numbers, math.random(#numbers))},
      {name, Card.Spade, table.remove(numbers, math.random(#numbers))},
    }
    room:changeCardArea(U.prepareDeriveCards(room, cards, kl__jijiao.name), Card.DrawPile)
    if player.dead then return end
    local hands = player:getCardIds("h")
    local colors = {}
    if #hands == 0 then return end
    for _, id in ipairs(hands) do
      table.insertIfNeed(colors, Fk:getCardById(id).color)
    end
    local card = table.filter(room.draw_pile, function(id)
      return Fk:getCardById(id).attack_range == #colors
    end)
    if #card > 0 then
      local id = table.random(card)
      room:obtainCard(player, id, false, fk.ReasonPrey, player, kl__jijiao.name)
      if not player.dead and table.contains(player:getCardIds("h"), id) and player:canUseTo(Fk:getCardById(id), player) then
        room:useCard{
          from = player,
          tos = {player},
          card = Fk:getCardById(id),
        }
      end
    end
  end,
})


return kl__jijiao
